#include "Base.h"
#include "MatchPlayer.h"
#include "DBProtocol.h"
#include "MWProtocol.h"
#include "WorldServer.h"
#include "Log.h"

int compWorldUserInfo(const void *arg1,const void *arg2);

CMatchPlayer::CMatchPlayer(void)
{
}

CMatchPlayer::~CMatchPlayer(void)
{
}

Msg* CMatchPlayer::CreateMsg(DWORD dwMsgId, int nLen)
{
	switch(dwMsgId)
	{
    case Msg_MW_User_Signup:
		return new MWUserSignupMsg;
	default:
		break;
	}
    return NULL;
}

BOOL CMatchPlayer::OnMsg(Msg *pMsg)
{
	DWORD dwMsgId = pMsg->GetMsgId();
	switch(dwMsgId)
	{
	case Msg_MW_User_Signup:
		{
	        OnUserSingup((MWUserSignupMsg*)pMsg);
			break;
		}
	default:
		break;
	}
    return TRUE;
}

BOOL CMatchPlayer::OnUserSingup(MWUserSignupMsg* pMsg)
{
	int nGroup = pMsg->m_dwUserId % User_Group_Count;
	g_pWorldServer->m_arrLock[nGroup].Lock();
	WorldUserInfo *pWorldUserInfo = g_pWorldServer->GetWorldUserInfo(pMsg->m_dwUserId);
	if(pWorldUserInfo == NULL)
	{
		pWorldUserInfo = new WorldUserInfo;
		pWorldUserInfo->m_dwUserId = pMsg->m_dwUserId;
		g_pWorldServer->m_arrlistUserInfo[nGroup].Insert(pWorldUserInfo, compWorldUserInfo);
	}

	if(pMsg->m_bSignup)
	{
        pWorldUserInfo->AddSignupInfo(pMsg->m_dwProductId, pMsg->m_tmMatchStartTime); 
	}
	else
	{
		pWorldUserInfo->DelSignupInfo(pMsg->m_dwProductId, pMsg->m_tmMatchStartTime); 
	}
	WriteLog("OnUserSingup bSignup=%d UserId=%d, ProductId=%d, MatchStartTime=%d", pMsg->m_bSignup, pMsg->m_dwUserId, pMsg->m_dwProductId, pMsg->m_tmMatchStartTime);
	g_pWorldServer->m_arrLock[nGroup].Unlock();
	return TRUE;
}
